package com.javatiaocao.myblog.redis;

import com.javatiaocao.myblog.mapper.VisitorMapper;
import com.javatiaocao.myblog.service.impl.HashRedisServiceImpl;
import com.javatiaocao.myblog.service.impl.RedisServiceImpl;
import com.javatiaocao.myblog.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.Set;

@Component
public class redisVisitorTask {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    VisitorMapper visitorMapper;
    @Autowired
    private RedisServiceImpl redisService;
    @Autowired
    private HashRedisServiceImpl hashRedisService;

//    @Scheduled(cron="0/5 * *  * * ? ")

    //    @Scheduled(cron = "0 0 0 * * ? ")
//    public void saveNumToMysql(){
//        int totalVisitor = (int)redisTemplate.opsForHash().get("visitor","totalVisitor");
//        int pageVisitor = (int)redisTemplate.opsForHash().get("visitor","visitorVolume");
//        int oldTotalVisitor = (int) visitorMapper.getVisitorNum("totalVisitor").getVisitorNum();
//        int yesterdayVisitor = totalVisitor - oldTotalVisitor;
//        Set visitorKey = redisTemplate.opsForHash().keys("visitor");
//        //遍历visitor中所有的key
//        for (Object o : visitorKey) {
//            if(o.equals("totalVisitor") || o.equals("visitorVolume") || o.equals("yesterdayVisitor")){
//                continue;
//            }
//            visitorMapper.updateVisitorNum((int)redisTemplate.opsForHash().get("visitor",o),(String) o);
//            redisTemplate.opsForHash().delete("visitor", o);
//        }
//        visitorMapper.updateVisitorNum(totalVisitor,"totalVisitor");
//        visitorMapper.updateVisitorNum(pageVisitor,"visitorVolume");
//        redisTemplate.opsForHash().put("visitor","visitorVolume",0);
//        redisTemplate.opsForHash().put("visitor","totalVisitor",visitorMapper.getVisitorNum("totalVisitor").getVisitorNum());
//    }
//    @Scheduled(cron = "0 0 0 * * ? ")
    @Scheduled(cron="0 0/5 * * * ?  ")
    public void statisticsVisitorNum() {
        long oldTotalVisitor = visitorMapper.getTotalVisitorNum();
        //查询今日的总人数
        long newTotalVisitor = Long.valueOf(hashRedisService.get(StringUtil.VISITOR, "totalVisitor").toString());
        //昨日访问量计算
        long yesterdayVisitor = newTotalVisitor - oldTotalVisitor;
        if (hashRedisService.hasHashKey(StringUtil.VISITOR,"yesterdayVisitor")){
            hashRedisService.put(StringUtil.VISITOR,"yesterdayVisitor",yesterdayVisitor);
        }else {
            hashRedisService.put(StringUtil.VISITOR,"yesterdayVisitor",oldTotalVisitor);
        }
        //redis to mysql
        LinkedHashMap linkedHashMap = (LinkedHashMap) hashRedisService.getAllFieldAndValue(StringUtil.VISITOR);
        String pageName;
        for (Object o : linkedHashMap.keySet()) {
            pageName =String.valueOf(o);
            visitorMapper.updateVisitorNum((Integer) linkedHashMap.get(pageName),pageName);
            if(!"totalVisitor".equals(pageName) && !"yesterdayVisitor".equals(pageName)){
                hashRedisService.hashDelete(StringUtil.VISITOR,pageName);
            }
        }
    }

}
